/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.sessionbean;

import java.util.Calendar;
import java.util.Date;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import mrp.util.exception.ATPFailedException;
import mrp.util.exception.MRPException;
import sales.entity.LineItem;
import sales.entity.SalesOrderEntity;

/**
 *
 * @author ReinForceII
 */
@Stateless
@LocalBean
public class MRPATPSessionBean {

    public void aTPCheck (SalesOrderEntity salesOrder) throws MRPException {
        boolean flag = false;
        Calendar daysLater = Calendar.getInstance();
        daysLater.add(Calendar.DAY_OF_MONTH, 15);
        LineItem failedItem = null;
        
        
        for (LineItem item : salesOrder.getSalesOrderLineItems())
        {
            
            if (item.getDeliveryDate().before(daysLater.getTime())){
                flag = true;
                failedItem = item;
                break;
            }
        }
        if (flag)
            throw new ATPFailedException(failedItem.getProduct().getProductName() + "cannnot be delivered on" + failedItem.getDeliveryDate().toString());
    }
    
}
